
// 1. 下载 vue-router 模块
//   yarn add vue-router
// 2. 按需导入模块
import Vue from 'vue';
import VueRouter from 'vue-router'
import {getToken} from '../utils/localData'
// 3. 注册路由组件，可以用到 <router-view></router-view> 和 <router-link></router-link>
Vue.use(VueRouter) 


// 4. 路由映射关系(路径与组件)
import Login from '../views/Login.vue'
import Home from '../views/Home.vue'
const routes = [
  { path: '/login', component: Login },
  { path: '/home', component: Home },
]

// 5. 创建路由实例
const router = new VueRouter({
  routes: routes
})

router.beforeEach((to, from, next) => {
  if(to.path==='/login'){
    next()
  }else{
    // 其他页面都要检查，有没有Tokey
    if(getToken()){
      next()
    }else{
      // next({path:'/login'})
      next('/login')
    }
  }
  
})

// 6.1 导出路由实例
export default router;